package 品牌管理;

import java.sql.*;
import java.util.ArrayList;

public class 查询全部品牌 {
    public static void main(String[] args) {
        //创建list存放品牌  泛型
        ArrayList<Brand> brandList = new ArrayList<>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
//        1.加载驱动Driver.class
            Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器（DriverManager）连接数据库
            String url = "jdbc:mysql://localhost:3306/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
            String user = "root";
            String pass = "0000";
            conn = DriverManager.getConnection(url, user, pass);
//        3.书写SQL语句，并执行
            String sql = "SELECT * FROM t_brand";
//        4.创建执行SQL的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
//        5.从rs中取出结果
            while(rs.next()){
                int id = rs.getInt("id");
                String brandName = rs.getString("brand_name");
                String companyName = rs.getString("company_name");
                int orderer = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
//                System.out.println(id+brandName+companyName+orderer+description+status);
                //封装对象
                Brand b = new Brand(id, brandName, companyName, orderer, description, status);
                brandList.add(b);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //        6.关闭连接
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //访问list  增强for
        for (Brand brand : brandList) {
            System.out.println(brand);
        }
    }
}
